//
//  UserAccountViewModel.swift
//  微博项目
//
//  Created by mac on 16/1/15.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {

    //用户账号的模型属性
    var userAccount: UserAccount?
    
    override init(){
        //用缓存的数据  给模型赋值
    userAccount = UserAccount.loadUserAccount()
        super.init()
    }
    
    //用户是否登录的标记
    var userLogin:Bool{
        //根据token 是否为空 来判断用户是否登录
    return userAccount?.access_token != nil
    }
    
    //从缓存获取token
    var token : String?{
    return userAccount?.access_token
    }
    
    //用户名称
    var userName : String?{
    return userAccount?.name
    }
    //用户头像的url
    var headImageURL: NSURL?{
    
    return NSURL(string: userAccount?.avatar_large ?? "")
        
    }
    
    
    //获取授权的token
    func loadAccessToken(code: String,finished: (isSuccess: Bool) -> ()){
    
    let urlString = "https://api.weibo.com/oauth2/access_token"
    let parameters = ["client_id":client_id,"client_secret": client_secret, "grant_type":"authorization_code","code": code,"redirect_uri": redirect_uri]
        
        let AFN = AFHTTPSessionManager()
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        //设置支持
        AFN.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            print(result)
            
           guard let dict = result as? [String : AnyObject] else{
            print("数据格式不正确")
            //执行失败的回调
            finished(isSuccess: false)
            return
            
            }
            let account = UserAccount(dict: dict)
            //加载用户信息
            self.loadUserInfo(account , finished:finished)
            
        
            }) { (_, error) -> Void in
                finished(isSuccess: false)
                print(error)
        }
    }
    
    //MARK:加载用户信息
    private func loadUserInfo(account:UserAccount ,finished: (isSuccess: Bool) -> ()){
    
        //准备URL
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parameter = ["access_token":account.access_token!,"uid":account.uid!]
        
        let AFN = AFHTTPSessionManager()
        AFN.GET(urlString, parameters: parameter, progress: nil, success: { (_, result) -> Void in
            print(result)
            guard let dict = result as? [String: AnyObject] else{
            
            print("数据格式不合法")
                finished(isSuccess: false)
                return
            }
            let name = dict["name"] as! String
            let avatar_large = dict["avatar_large"] as! String
            
            //我们需要的用户信息就全部获取到了
            account.name = name
            account.avatar_large = avatar_large
            print(account)
            account.saveAccount()
            
            //执行成功 的回调
            finished(isSuccess: true)
            
            }) { (_, error) -> Void in
                
                //失败的回调
                finished(isSuccess: false)
                print(error)
        }
        
    
    }
}
